package org.com.system.util.utils;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


import org.apache.commons.beanutils.BeanUtils;
import org.com.system.dept.Dept;
import org.com.system.util.model.ActionValues;
import org.com.system.util.model.Model;
import org.com.system.util.sql.SQLUtils;

public class ModelUtils {

	/**
	 * 得到类中字段数量
	 * @param model
	 * @return
	 */
	public static int getFieldNumber(Class<?> model){
		int num=0;
		Field[] fieldsClass=model.getDeclaredFields();
		if(null!=fieldsClass){
			num=fieldsClass.length;
		}
		return num;
	}
	
	/**
	 * 判断实体类里是否有此字段
	 * @param model
	 * @return
	 */
	public static boolean hasField(String fieldName,Class<?> model){
		boolean have=false;
		String[] fields=getFieldNames(model, false);
		for(String field:fields){
		    if(field.equalsIgnoreCase(fieldName)){
			    have=true;		    		
			}
		}
		return have;
	}
	
	
	/**
	 * 得到实体中某个字段的值
	 * @param fieldName
	 * @param bean
	 * @return
	 */
	public static String getFieldValue(String fieldName,Object bean){
		String fieldValue=null;
		try{
			fieldValue=BeanUtils.getSimpleProperty(bean, fieldName);
		}catch(Exception e){
			e.printStackTrace();
		}
		return fieldValue;
	}
	
	
	/**
	 * 得到实体对象中的字段
	 * @param model
	 * @return String[]
	 */
	public static String[] getFieldNames(Class<?> model,boolean joinSuper){
		String[] names=null;
		List<Field> fields=null;
		if(null!=model){
			fields=new ArrayList<Field>();
		    Field[] fieldsClass=model.getDeclaredFields();
		    Field[] fieldsSuperClass=new Field[0];
		    if(joinSuper&&null!=model.getSuperclass()&&model!=Model.class){
		    	fieldsSuperClass=model.getSuperclass().asSubclass(Model.class).getDeclaredFields();
		    }
		    fields.addAll(Arrays.asList(fieldsClass));
		    fields.addAll(Arrays.asList(fieldsSuperClass));
		    names=new String[fieldsClass.length+fieldsSuperClass.length];
		    int i=0;
		    for(Field f:fields){
		    	names[i]=f.getName();
		    	i++;
		    }
		}
		return names;
	}
	
	/**
	 * 从结集中Copy值构成实体对象
	 * @param result
	 * @param model
	 * @return Object
	 */
	public static Object copyFromResult(ResultSet result,Class<?> model){
		Object mobj=null;
		try{
			mobj=model.newInstance();
			String[] names=getFieldNames(model,false);
			for(String name:names){
				BeanUtils.setProperty(mobj, name, result.getString(SQLUtils.addUnderline(name)));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return mobj;
	}
	
	/**
	 * 从实体对象中Copy值到ActionValues中
	 * @param bean
	 * @return
	 */
	public static ActionValues<String,Object> copyFromObject(Object bean){
		ActionValues<String,Object> values=null;
		try{
		    if(null!=bean){
			    values=new ActionValues<String,Object>();
			    String[] filedNames=getFieldNames(bean.getClass(),false);
			    for(String name:filedNames){
				    values.put(name,BeanUtils.getProperty(bean, name));
			    }
		    }
		}catch(Exception e){
			e.printStackTrace();
		}
		return values;
	}
	
	/**
	 * 从ActionValues中Copy值到实体对象
	 * @param bean
	 * @return
	 */
	public static Object copyFromMap(Map<String,Object> values,Class<?> model){
		Object obj=null;
		if(null!=values&&values.size()>0){
			try{
			    obj=model.newInstance();
			    BeanUtils.copyProperties(obj, values);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return obj;
	}
	
	/**
	 * 得到所有字段的值数组
	 * @param args
	 */
	public static String[] getFieldValues(Class<?> model,Object bean){
		String[] fieldValues=null;
		String[] fieldNames=ModelUtils.getFieldNames(model, false);
		if(CollectionUtils.isNotEmpty(fieldNames)){
			int size=fieldNames.length;
			fieldValues=new String[size];
			for(int i=0;i<size;i++){
				fieldValues[i]=getFieldValue(fieldNames[i], bean);
			}
		}
		return fieldValues;
	}
	
	public static void main(String[] args) {
		String[] fields=ModelUtils.getFieldNames(Dept.class, true);
		for(String name:fields){
			System.out.println(name);
		}
		
	}
	
}
